package com.fastjrun.eladmin.itrms.service.impl;


import com.fastjrun.eladmin.itrms.common.exception.ServiceException;
import com.fastjrun.eladmin.itrms.dao.RmsProjectSystemDao;
import com.fastjrun.eladmin.itrms.dto.ProjectSystemDTO;
import com.fastjrun.eladmin.itrms.dto.ProjectSystemQueryDTO;
import com.fastjrun.eladmin.itrms.entity.RmsProjectSystem;
import com.fastjrun.eladmin.itrms.entity.RmsProjectSystemExample;
import com.fastjrun.eladmin.itrms.mapstruct.RmsProjectSystemMapper;
import com.fastjrun.eladmin.itrms.service.RmsProjectSystemControllerService;
import com.fastjrun.eladmin.itrms.utils.PageUtils;
import com.fastjrun.eladmin.itrms.vo.ProjectSystemVO;
import com.fastjrun.eladmin.vo.PageResult;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.SecurityUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("rmsProjectSystemControllerService")
public class RmsProjectSystemControllerServiceImpl extends BaseItrmsService implements RmsProjectSystemControllerService {
    @Resource
    RmsProjectSystemDao rmsProjectSystemDao;

    @Resource
    RmsProjectSystemMapper rmsProjectSystemMapper;

    private RmsProjectSystemExample retrive(Long projectId,ProjectSystemQueryDTO queryDTO) {
        log.debug("queryDTO={}", queryDTO);
        RmsProjectSystemExample rmsProjectSystemExample = new RmsProjectSystemExample();
        RmsProjectSystemExample.Criteria criteria=rmsProjectSystemExample.createCriteria();
        if(projectId!=null){
            criteria.andProjectIdEqualTo(projectId);
        }
        if(queryDTO!=null){
            if(!StringUtils.isBlank(queryDTO.getSystemName())){
                criteria.andSystemNameLike(queryDTO.getSystemName());
            }
        }

        return rmsProjectSystemExample;
    }

    @Override
    public void export(Long projectId, HttpServletResponse response, ProjectSystemQueryDTO queryDTO) {
        RmsProjectSystemExample rmsProjectSystemExample = this.retrive(projectId,queryDTO);
        List<RmsProjectSystem> all = rmsProjectSystemDao.selectByExampleWithBLOBs(rmsProjectSystemExample);
        List<ProjectSystemVO> projectSystemVOS = this.rmsProjectSystemMapper.toVO(all);
        List<Map<String, Object>> list = Lists.newArrayList();
        for (RmsProjectSystem projectSystem : all) {
            Map<String, Object> map = Maps.newHashMap();
            map.put("系统编号", projectSystem.getSystemNo());
            map.put("系统名称", projectSystem.getSystemName());
            map.put("系统版本", projectSystem.getSystemVersion());
            map.put("备注", projectSystem.getRemark());
            map.put("负责用户ID", projectSystem.getOwnerUserId());
            map.put("创建用户ID", projectSystem.getCreateUserId());
            map.put("负责用户", projectSystem.getOwnerUser());
            map.put("创建用户", projectSystem.getCreateUser());
            map.put("创建时间", projectSystem.getCreateDate());
            map.put("更新时间", projectSystem.getUpdateDate());
            list.add(map);
        }
        try {
            FileUtil.downloadExcel(list, response);
        } catch (IOException e) {
            log.warn("", e);
            throw new ServiceException(e);
        }
    }

    @Override
    public PageResult<ProjectSystemVO> queryAll(ProjectSystemQueryDTO queryDTO, Pageable pageable) {
        return this.query(null,queryDTO,pageable);
    }

    @Override
    public PageResult<ProjectSystemVO> query(Long projectId, ProjectSystemQueryDTO queryDTO, Pageable pageable) {
        if (pageable == null) {
            pageable = PageRequest.of(PAGE_CURRENT, PAGE_SIZE);
        }


        PageUtils.startPage(pageable);

        RmsProjectSystemExample rmsProjectSystemExample = this.retrive(projectId,queryDTO);String orderClause=this.getOrderClause(pageable);
        if(StringUtils.isNotBlank(orderClause)){
            rmsProjectSystemExample.setOrderByClause(orderClause);
        }

        PageInfo<RmsProjectSystem> pageInfo = new PageInfo<>(rmsProjectSystemDao.selectByExampleWithBLOBs(rmsProjectSystemExample));
        List<ProjectSystemVO> list = rmsProjectSystemMapper.toVO(pageInfo.getList());

        PageResult<ProjectSystemVO> pageResult = new PageResult<>();
        pageResult.setContent(list);
        pageResult.setTotalElements((int) pageInfo.getTotal());
        return pageResult;
    }

    @Override
    public void create(ProjectSystemDTO createDTO) {
        RmsProjectSystem rmsProjectSystem = this.rmsProjectSystemMapper.toEntity(createDTO);
        rmsProjectSystem.setCreateUser(SecurityUtils.getCurrentUsername());
        rmsProjectSystem.setCreateUserId(SecurityUtils.getCurrentUserId());
        rmsProjectSystem.setOwnerUser(sysUserDao.selectByPrimaryKey(createDTO.getOwnerUserId()).getUsername());
        rmsProjectSystem.setCreateDate(new Date());
        rmsProjectSystem.setUpdateDate(new Date());
        rmsProjectSystemDao.insert(rmsProjectSystem);

    }

    @Override
    public void update(ProjectSystemDTO updateDTO) {
        RmsProjectSystem rmsProjectSystem = this.rmsProjectSystemMapper.toEntity(updateDTO);
        rmsProjectSystem.setOwnerUser(sysUserDao.selectByPrimaryKey(updateDTO.getOwnerUserId()).getUsername());
        rmsProjectSystem.setUpdateDate(new Date());
        rmsProjectSystemDao.updateByPrimaryKeySelective(rmsProjectSystem);

    }

    @Override
    public void delete(Long[] ids) {
        RmsProjectSystemExample rmsProjectSystemExample = new RmsProjectSystemExample();
        rmsProjectSystemExample.createCriteria().andIdIn(Arrays.asList(ids));
        rmsProjectSystemDao.deleteByExample(rmsProjectSystemExample);
    }
}
